﻿<?php
include_once("../initial.php");
include_once("../dbdriver/commitWHandle.php");
include_once("../dbdriver/commitRHandle.php");
include_once("machineLogic.php");
include_once("userLogic.php");
include_once("roleLogic.php");
include_once("machinenumLogic.php");

class commitLogic
{
    public function insert_commit($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new commitWHandle();
        return $whandle->insert_data($array);
    }
    public function get_commit($array='', $orderby='', $limit='')
    {
        $rhandle = new commitRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array, $orderby, $limit);
        }
    }
    public function get_commit_num($array='')
    {
        $rhandle = new commitRHandle();
        if(is_array($array)==false)
        {
            $array = array();
        }
        return $rhandle->get_commit_num($array);
    }
    public function update_commit($array,$id)
    {
        $handle = new commitWHandle();
        return $handle->update_commit($array,$id);
    }
    public function delete_commit($id)
    {
        $handle = new commitWHandle();
        return $handle->delete_commit($id);
    }
    public function get_all_commit($orderby="", $limit="")
    {
        $handle = new commitRHandle();
        if($orderby=="")
        {
            $orderby="order by bookdate desc";
        }
        return $handle->get_all_data($orderby,$limit);
    }
    public function get_pass_commit($orderby="", $limit="")
    {
        $array=array();
        $array['pass']="Y";
        $handle = new commitRHandle();
        return $handle->get_some_data($array,"order by bookdate desc",$limit);
    }
    public function get_reject_commit($orderby="", $limit="")
    {
        $array=array();
        $array['pass']="R";
        $handle = new commitRHandle();
        if($orderby=="")
        {
            $orderby="order by bookdate desc";
        }
        return $handle->get_some_data($array,$orderby,$limit);
    }
    public function get_wait_commit($orderby="", $limit="")
    {
        $array=array();
        $array['pass']="N";
        $handle = new commitRHandle();
        if($orderby=="")
        {
            $orderby="order by bookdate desc";
        }
        return $handle->get_some_data($array,$orderby,$limit);
    }
    public function get_user_all_commit($uid)
    {
        $array=array();
        $array['uid']=$uid;
        $handle = new commitRHandle();
        return $handle->get_some_data($array,"order by bookdate desc","");
    }
    public function get_user_pass($uid)
    {
        $array = array();
        $array['uid']=$uid;
        $array['pass']="Y";
        $handle = new commitRHandle();
        return $handle->get_some_data($array, "order by bookdate desc","");
    }
    public function get_user_reject($uid)
    {
        $array=array();
        $array['uid']=$uid;
        $array['pass']="R";
        $handle = new commitRHandle();
        return $handle->get_some_data($array, "order by bookdate desc","");
    }
    public function get_user_wait($uid)
    {
        $array=array();
        $array['uid']=$uid;
        $array['pass']="N";
        $handle = new commitRHandle();
        return $handle->get_some_data($array, "order by bookdate desc","");
    }
    public function search_commit($machinename="", $machineid="", $userid="", $time="")
    {
        $array=array();
        $handle = new commitRHandle();
        return $handle->search_commit($machinename,$machineid,$userid,$time);
    }
    public function search_by_book_time($time="")
    {
        $handle = new commitRHandle();
        return $handle->search_by_book_time($time);
    }
    
    public function search_machine_user($mid)
    {
        //查看机器的使用历史
        $handle = new commitRHandle();
        
        return $handle->get_some_data(array("mid"=>$mid), "order by machinedate desc", "limit 20");
    }
    
    public function search_by_machine_date($mid, $year, $month, $day)
    {
        $machinedate=$year."-".$month."-".$day;
        $handle = new commitRHandle();
        return $handle->get_some_data(array("mid"=>$mid,"machinedate"=>$machinedate),"order by machinedate desc", "");
    }
    
    //审核通过一个预约
    public function pass_one_commit($id,$adminuserid=0)
    {
        if($id=="")
        {
            return false;
        }
        $array = array("pass"=>"Y","checkid"=>$adminuserid,"checktime"=>date("Y-m-d H:i:s"));
        $this->update_commit($array,$id);
        return true;
    }
    public function reject_one_commit($id,$adminuserid=0)
    {
        if($id=="")
        {
            return false;
        }
        $array = array("pass"=>"R","checkid"=>$adminuserid,"checktime"=>date("Y-m-d H:i:s"));
        $this->update_commit($array,$id);
        
        //更新机器数目
        $ml = new machineLogic();
        
        $commitinfo = $this->get_commit(array("id"=>$id));
        if(count($commitinfo)<=0)
        {
            return false;
        }
        
        //更新机器次数
        $usertime=0;
        $machineinfo = $ml->get_machine(array("id"=>$commitinfo[0]['mid']));
        if($machineinfo[0]['type']=="0")
        {
            $mnuml = new machinenumLogic();
            $machineremaininfo = $mnuml->get_machinenum(array("mid"=>$machineinfo[0]['id'],"date"=>$commitinfo[0]['machinedate']));
            
            $booktime = $commitinfo[0]['booktime'];
            $machinetime = $machineremaininfo[0]['remain']+$booktime;
            $usertime = $booktime;
            $mnuml->update_machinenum(array("remain"=>$machinetime),$machineremaininfo[0]['id']);
            //$ml->update_machine(array("remain"=>$machinetime),$machineinfo[0]['id']);
        } 
        else
        {
            $mnuml = new machinenumLogic();
            $machineremaininfo = $mnuml->get_machinenum(array("mid"=>$machineinfo[0]['id'],"date"=>$commitinfo[0]['machinedate']));
            
            $booktime = unserialize($commitinfo[0]['booktime']);
            $machinetime = unserialize($machineremaininfo[0]['remain']);
            $usertime = count($booktime);
            foreach($booktime as $key=>$value)
            {
                $machinetime[$key]=1;
            }
            $mnuml->update_machinenum(array("remain"=>serialize($machinetime)),$machineremaininfo[0]['id']);  

        }
        //更新机器数目结束
        
        return true;
    }
    
    //撤销一个已经存在的预约
    public function cansel_one_commit($id)
    {
        if($id=="")
        {
            return false;
        }
        
        $ml = new machineLogic();
        $rl = new roleLogic();
        $ul = new userLogic();
        
        $commitinfo = $this->get_commit(array("id"=>$id));
        if(count($commitinfo)<=0)
        {
            return false;
        }
        //撤单 
        if($commitinfo[0]['check']=="N" || $commitinfo[0]['pass']=="Y" || $commitinfo[0]['pass']=='R')
        {
            //已经撤销过了
            $_SESSION['notice']="已经被管理员处理了，不能撤销";
            return false;
        } 
        $this->delete_commit($id);
        
        //更新机器次数
        $usertime=0;
        $machineinfo = $ml->get_machine(array("id"=>$commitinfo[0]['mid']));
        if($machineinfo[0]['type']=="0")
        {
            $mnuml = new machinenumLogic();
            $machineremaininfo = $mnuml->get_machinenum(array("mid"=>$machineinfo[0]['id'],"date"=>$commitinfo[0]['machinedate']));
            
            $booktime = $commitinfo[0]['booktime'];
            $machinetime = $machineremaininfo[0]['remain']+$booktime;
            $usertime = $booktime;
            $mnuml->update_machinenum(array("remain"=>$machinetime),$machineremaininfo[0]['id']);
            //$ml->update_machine(array("remain"=>$machinetime),$machineinfo[0]['id']);
        } 
        else
        {
            $mnuml = new machinenumLogic();
            $machineremaininfo = $mnuml->get_machinenum(array("mid"=>$machineinfo[0]['id'],"date"=>$commitinfo[0]['machinedate']));
            
            $booktime = unserialize($commitinfo[0]['booktime']);
            $machinetime = unserialize($machineremaininfo[0]['remain']);
            $usertime = count($booktime);
            foreach($booktime as $key=>$value)
            {
                $machinetime[$key]=1;
            }
            $mnuml->update_machinenum(array("remain"=>serialize($machinetime)),$machineremaininfo[0]['id']);  

        }
        
        //更新角色中的次数
        $roleinfo = $rl->get_role(array("id"=>$commitinfo[0]['rid']));
        $allutime = unserialize($roleinfo[0]['all_utime']);
        $allutime[$commitinfo[0]['uid']] = $allutime[$commitinfo[0]['uid']]+$usertime;
        $rl->update_role(array("all_utime"=>serialize($allutime)),$commitinfo[0]['rid']);
        
        //更新用户次数
        $userinfo = $ul->get_user(array("id"=>$commitinfo[0]['uid']));
        $booktime = $userinfo[0]['booktime']+$usertime;
        $ul->update_user(array("booktime"=>$booktime),$commitinfo[0]['uid']);
        return true;
    }
    
}
?>
